package per.lee.spingboothelper.db.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import per.lee.spingboothelper.db.config.ColumnValueConfig;
import per.lee.spingboothelper.db.converter.DescConvertColumn;
import per.lee.spingboothelper.db.model.Column;
import per.lee.spingboothelper.db.model.Table;
import per.lee.spingboothelper.db.sql.DataCreater;

@RestController
@RequestMapping("/batchInsertRandom")
public class BatchInsertRandomController {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	@RequestMapping("/mockData")
	public void mockData() {

//		ColumnValueConfig.TABLE_LIST.stream().filter(t->t.equals("ads_brc_rpt_born_day")).forEach(t -> {
		ColumnValueConfig.TABLE_LIST.stream().forEach(t -> {
			Table table = new Table();
			table.setTableName(t);

			List<Map<String, Object>> queryForList = jdbcTemplate.queryForList("desc " + t);
//			queryForList.forEach(x->{
//				System.out.println(x);
//			});
			List<Column> columns = DescConvertColumn.convert(queryForList);
			table.setColumns(columns);

			List<String> sqlList = DataCreater.createBatchInsertSql(table);
			for (String sql : sqlList) {
				try {
					jdbcTemplate.update(sql);
				} catch (Exception e) {
					System.err.println(e.getMessage());
				}			
			}
		});
	}
	
	@RequestMapping("/truncate")
	public void truncate() {
		ColumnValueConfig.TABLE_LIST.forEach(t -> {
			jdbcTemplate.update("truncate "+t);
		});
	}
	
	public static void main(String[] args) {
		Table table = new Table();
		table.setTableName("person");
		List<Column> columns = new ArrayList<Column>();
		columns.add(new Column("dt","int(11)"));
		columns.add(new Column("dt_year","varchar(11)"));
		columns.add(new Column("dt_month","varchar(11)"));
		columns.add(new Column("dt_day","varchar(11)"));
		columns.add(new Column("farm_id","varchar(11)"));
		table.setColumns(columns);
		
		List<String> sqlList = DataCreater.createBatchInsertSql(table);
		for (String sql : sqlList) {
			System.out.println(sql);
		}
		
	}

}